// pages/car_company/orderAdd/orderAdd.js
var Tool = require("../../../utils/ztool.js");
var tool = new Tool();
var util = require('../../../utils/util.js');
Page({
  data: {
    isadd: 1,
    items: [
      { name: 'YES', value: '是', checked: true },
      { name: 'NO', value: '否', checked: false },
    ],
    items2: [
      { name: 'YES', value: '是', checked: true },
      { name: 'NO', value: '否', checked: false },
    ],
    usecarcomcansee: true,
    includeprice: true,
    routetitle: '',
    usecarcompanylist: [],
    arroriginal: [],
    testboolean: false,
    usecarcompanuname: '',
    usecarcompanutel: '',
    usecarcompanuperson: '',
    lxsidSecondary: '',
    upplace: '',
    downplace: '',
    routeseatcount: '',
    peoplecount: '',
    daycount: '1',
    starttime: '',
    startdate: '',
    enddate: '',
    endtime: '',
    routeprice: '',
    routeroute: '',
    routeother: '',
    pagetype: 1,//1是新增，2是修改
    IDD: '',
    delectfricom: false,
    addfricom: false,
    addfricom2: false,
    inputchangeforadd: '',
    addfriendcomplist: [],
    arroriginaladdfriendlist: [],
    sendhidden: false,
    arrcompare2: [],//初始的数组
    arrlatest: [],//修改完的数组
    dateAndroute1: [],//行程和日期数组
    selectCompanyAdd: 0,
    //主页面显示的内容
    usecarcompanupersonmain: '',
    usecarcompanutelmain: '',
    usecarcompanunamemain: '',
    lxsid: '',
    //手动输入的用车单位
    usecarcompanuperson2: '',
    usecarcompanutel2: '',
    usecarcompanuname2: '',

    timeCheck1: false, //开始时间是否选上
    timeCheck2: false,  //结束时间是否选上
  },

  switchChange1: function (e) {
    var that = this
    if (e.detail.value == true) {
      that.setData({
        timeCheck1: true
      })
    } else {
      that.setData({
        timeCheck1: false
      })
    }
    console.log(that.data.timeCheck1)
  },

  switchChange2: function (e) {
    var that = this
    if (e.detail.value == true) {
      that.setData({
        timeCheck2: true
      })
    } else {
      that.setData({
        timeCheck2: false
      })
    }
    console.log(that.data.timeCheck2)
  },

  //获取订单细节
  getdetail: function (IDD) {
    var detail = []
    var that = this
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CarCompany/CarCompOrders.ashx',
      data: {
        IDD: IDD,
      },
      method: 'GET',
      success: function (res) {
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          that.setData({
            loadError: true
          });
          return
        }
        var data = tool.getRegularJson(res.data);
       
        console.log(data)

        if (data.code != 0) {
          that.showError(data.msg)
        }

        if (data.data.length > 0) {
          detail = data.data[0]
          //修改操作时要加载数据
          var startdate = detail.UseCarStartTime.substring(0, 10)
          var enddate = detail.UseCarEndTime.substring(0, 10)
          var starttime = detail.UseCarStartTime.substring(11, 16)
          var endtime = detail.UseCarEndTime.substring(11, 16)

          that.setData({
            IDD: detail.IDD,
            routetitle: detail.routeTitle,
            usecarcompanunamemain: detail.UserCompanyName,
            usecarcompanutelmain: detail.Telphone,
            usecarcompanupersonmain: detail.RealName,
            usecarcomcansee: detail.showUser,
            upplace: detail.GetOnPlace,
            downplace: detail.GetOffPlace,
            routeseatcount: detail.seatRequire,
            peoplecount: detail.peopleRequire,
            daycount: detail.DayNumber,
            startdate: startdate,
            starttime: starttime,
            enddate: enddate,
            endtime: endtime,
            routeprice: detail.QuotePrice,
            routeroute: detail.Route,
            routeother: detail.OtherRequire,
            includeprice: detail.inclued,
          })
          //将数据放入一个数组，在保存的时候判断是否有过订单变动
          var arrcompare = []
          arrcompare = arrcompare.concat(that.data.routetitle)
          arrcompare = arrcompare.concat(that.data.usecarcompanunamemain)
          arrcompare = arrcompare.concat(that.data.usecarcompanutelmain)
          arrcompare = arrcompare.concat(that.data.usecarcomcansee)
          arrcompare = arrcompare.concat(that.data.upplace)
          arrcompare = arrcompare.concat(that.data.downplace)
          arrcompare = arrcompare.concat(that.data.routeseatcount)
          arrcompare = arrcompare.concat(that.data.peoplecount)
          arrcompare = arrcompare.concat(that.data.daycount)
          arrcompare = arrcompare.concat(that.data.startdate)
          arrcompare = arrcompare.concat(that.data.starttime)
          arrcompare = arrcompare.concat(that.data.enddate)
          arrcompare = arrcompare.concat(that.data.endtime)
          arrcompare = arrcompare.concat(that.data.routeprice)
          arrcompare = arrcompare.concat(that.data.routeroute)
          arrcompare = arrcompare.concat(that.data.routeother)
          if (that.data.includeprice == 1) {
            arrcompare = arrcompare.concat('含费')
          } else {
            arrcompare = arrcompare.concat('不含费')
          }
          // arrcompare = arrcompare.concat(this.data.includeprice)

          that.data.arrcompare2.push(arrcompare)
          console.log(that.data.arrcompare2)

          //初始的用车时间和行程,标题
          var dateAndroute = []
          dateAndroute = dateAndroute.concat(that.data.startdate)
          dateAndroute = dateAndroute.concat(that.data.starttime)
          dateAndroute = dateAndroute.concat(that.data.enddate)
          dateAndroute = dateAndroute.concat(that.data.endtime)
          dateAndroute = dateAndroute.concat(that.data.routeroute)
          dateAndroute = dateAndroute.concat(that.data.routetitle)
          that.data.dateAndroute1.push(dateAndroute)


          // console.log(this.data.hanfei)
          // console.log(this.data.showuser)
          if (that.data.includeprice == 0) {
            that.setData({
              items2: [
                { name: 'YES', value: '是', checked: false },
                { name: 'NO', value: '否', checked: true },
              ],
            })
          } else {
            that.setData({
              items2: [
                { name: 'YES', value: '是', checked: true },
                { name: 'NO', value: '否', checked: false },
              ],
            })
          }
          if (that.data.usecarcomcansee == 1) {
            that.setData({
              items: [
                { name: 'YES', value: '是', checked: true },
                { name: 'NO', value: '否', checked: false },
              ],
            })
          } else {
            that.setData({
              items: [
                { name: 'YES', value: '是', checked: false },
                { name: 'NO', value: '否', checked: true },
              ],
            })
          }

          if (that.data.starttime == '00:00' && that.data.endtime == '23:59') {
            that.setData({
              timeCheck1: false,
              timeCheck2: false
            })
          } else {
            that.setData({
              timeCheck1: true,
              timeCheck2: true,
            })
          }
          
        }
      },
      fail: function () {
        console.log('加载错误');
        that.setData({
          loadError: true
        });

      }
    })
    
  },

  onLoad: function (options) {
    console.log(options)
    this.setData({
      pagetype: options.pagetype,
    })

    if (this.data.pagetype == undefined || this.data.pagetype == '') {
      this.setData({
        pagetype: 1,
      })
    }

    // 页面初始化 options为页面跳转所带来的参数
    if (this.data.pagetype == 2) {
      this.getdetail(options.IDD)
      // //修改操作时要加载数据
      // var startdate = options.UseCarStartTime.substring(0, 10)
      // var enddate = options.UseCarEndTime.substring(0, 10)
      // var starttime = options.UseCarStartTime.substring(11, 16)
      // var endtime = options.UseCarEndTime.substring(11, 16)

      // this.setData({
      //   IDD: options.IDD,
      //   routetitle: options.route,
      //   usecarcompanunamemain: options.usecarcom,
      //   usecarcompanutelmain: options.usecarcomtel,
      //   usecarcompanupersonmain: options.RealName,
      //   usecarcomcansee: options.showUser,
      //   upplace: options.upplace,
      //   downplace: options.downplace,
      //   routeseatcount: options.seatRequire,
      //   peoplecount: options.peopleRequire,
      //   daycount: options.DayNumber,
      //   startdate: startdate,
      //   starttime: starttime,
      //   enddate: enddate,
      //   endtime: endtime,
      //   routeprice: options.QuotePrice,
      //   routeroute: options.Route,
      //   routeother: options.OtherRequire,
      //   includeprice: options.inclued,
      // })
      // //将数据放入一个数组，在保存的时候判断是否有过订单变动
      // var arrcompare = []
      // arrcompare = arrcompare.concat(this.data.routetitle)
      // arrcompare = arrcompare.concat(this.data.usecarcompanunamemain)
      // arrcompare = arrcompare.concat(this.data.usecarcompanutelmain)
      // arrcompare = arrcompare.concat(this.data.usecarcomcansee)
      // arrcompare = arrcompare.concat(this.data.upplace)
      // arrcompare = arrcompare.concat(this.data.downplace)
      // arrcompare = arrcompare.concat(this.data.routeseatcount)
      // arrcompare = arrcompare.concat(this.data.peoplecount)
      // arrcompare = arrcompare.concat(this.data.daycount)
      // arrcompare = arrcompare.concat(this.data.startdate)
      // arrcompare = arrcompare.concat(this.data.starttime)
      // arrcompare = arrcompare.concat(this.data.enddate)
      // arrcompare = arrcompare.concat(this.data.endtime)
      // arrcompare = arrcompare.concat(this.data.routeprice)
      // arrcompare = arrcompare.concat(this.data.routeroute)
      // arrcompare = arrcompare.concat(this.data.routeother)
      // if (this.data.includeprice == 1) {
      //   arrcompare = arrcompare.concat('含费')
      // } else {
      //   arrcompare = arrcompare.concat('不含费')
      // }
      // // arrcompare = arrcompare.concat(this.data.includeprice)

      // this.data.arrcompare2.push(arrcompare)
      // console.log(this.data.arrcompare2)

      // //初始的用车时间和行程,标题
      // var dateAndroute = []
      // dateAndroute = dateAndroute.concat(this.data.startdate)
      // dateAndroute = dateAndroute.concat(this.data.starttime)
      // dateAndroute = dateAndroute.concat(this.data.enddate)
      // dateAndroute = dateAndroute.concat(this.data.endtime)
      // dateAndroute = dateAndroute.concat(this.data.routeroute)
      // dateAndroute = dateAndroute.concat(this.data.routetitle)
      // this.data.dateAndroute1.push(dateAndroute)






      // // console.log(this.data.hanfei)
      // // console.log(this.data.showuser)
      // if (this.data.includeprice == 0) {
      //   this.setData({
      //     items2: [
      //       { name: 'YES', value: '是', checked: false },
      //       { name: 'NO', value: '否', checked: true },
      //     ],
      //   })
      // } else {
      //   this.setData({
      //     items2: [
      //       { name: 'YES', value: '是', checked: true },
      //       { name: 'NO', value: '否', checked: false },
      //     ],
      //   })
      // }
      // if (this.data.usecarcomcansee == 1) {
      //   this.setData({
      //     items: [
      //       { name: 'YES', value: '是', checked: true },
      //       { name: 'NO', value: '否', checked: false },
      //     ],
      //   })
      // } else {
      //   this.setData({
      //     items: [
      //       { name: 'YES', value: '是', checked: false },
      //       { name: 'NO', value: '否', checked: true },
      //     ],
      //   })
      // }

      // if (this.data.starttime == '00:00' && this.data.endtime == '23:59') {
      //   this.setData({
      //     timeCheck1: false,
      //     timeCheck2: false
      //   })
      // } else {
      //   this.setData({
      //     timeCheck1: true,
      //     timeCheck2: true,
      //   })
      // }




    } else {
      // 调用函数时，传入new Date()参数，返回值是日期和时间  
      var time1 = util.formatTime1(new Date());
      var time2 = util.formatTime2(new Date());
      // 再通过setData更改Page()里面的data，动态更新页面的数据  
      this.setData({
        startdate: time1,
        enddate: time1,
        starttime: '00:00',
        endtime: '23:00',
      });
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  radio: function (e) {
    console.log(e)
  },

  //获取用车单位
  getusecarcompany: function () {
    var that = this

    that.setData({
      usecarcompanylist: [],
      arroriginal: [],
      selectCompanyAdd: 0,
    });

    var useinfo = wx.getStorageSync('userInfo')
    var tel = useinfo.Telphone
    var pwd = wx.getStorageSync('password');

    wx.request({
      url: 'https://www.lanshuicar.com/Server/CarCompany/LxsPtners.ashx',
      data: {
        tel: tel,
        pwd: pwd,
        act: 'getl',
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length >= 0) {
          that.setData({
            usecarcompanylist: that.data.usecarcompanylist.concat(data.data),
            testboolean: true,
          });

          that.setData({
            arroriginal: that.data.usecarcompanylist,
          });
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  //输入框变化搜索
  inputchange: function (e) {
    var that = this
    var arr = []
    var keyWord = e.detail.value

    for (var i = 0; i < that.data.arroriginal.length; i++) {
      if (that.data.arroriginal[i].lxsName.indexOf(keyWord) >= 0) {
        arr.push(that.data.arroriginal[i]);
      }
    }
    if (keyWord != null && keyWord != undefined && keyWord != '' && keyWord != ' ' && keyWord.length != 0) {
      that.setData({
        usecarcompanylist: arr,
        // usecarcompanuname: keyWord,
      });
    } else {
      that.setData({
        usecarcompanylist: that.data.arroriginal
      });
    }
  },

  //取消操作
  onCancel: function () {
    this.hideModal();
  },

  hideModal: function () {
    this.setData({
      testboolean: false,
      arroriginal: [],
      usecarcompanylist: []
    });
  },

  inputusecarcompany: function () {
    this.setData({
      testboolean: false,
    });
  },

  clickusecarcompany: function (e) {
    var that = this
    console.log(e)
    var companyname = e.currentTarget.dataset.idx
    var companylist = that.data.arroriginal

    for (var i = 0; i < companylist.length; i++) {
      if (companylist[i].lxsName == companyname) {
        that.setData({
          usecarcompanuname: companyname,
          usecarcompanutel: companylist[i].Telphone,
          testboolean: false,
          lxsidSecondary: companylist[i].lxsID,
          usecarcompanuperson: companylist[i].BackUp4,
          selectCompanyAdd: 1,
        });
      }
    }

    that.confirmusercompany()
  },

  usecarcomcanseeradio: function (e) {
    var that = this
    console.log(e)
    var cansee = e.currentTarget.dataset.id
    if (cansee == 'NO') {
      that.setData({
        usecarcomcansee: false
      });
    } else {
      that.setData({
        usecarcomcansee: true
      });
    }

    console.log(that.data.usecarcomcansee)
  },

  inputchangeupplace: function (e) {
    var that = this
    console.log(e)

    that.setData({
      upplace: e.detail.value,
    });
  },

  inputchangedownplace: function (e) {
    var that = this
    console.log(e)

    that.setData({
      downplace: e.detail.value,
    });
  },

  inputchangetitle: function (e) {
    var that = this
    console.log(e)

    that.setData({
      routetitle: e.detail.value,
    });

    console.log(that.data.routetitle)
    console.log(that.data.routetitle.length)
  },

  inputchangeseatcount: function (e) {
    var that = this
    console.log(e)

    that.setData({
      routeseatcount: e.detail.value,
    });
  },

  inputchangepeoplecount: function (e) {
    var that = this
    console.log(e)

    that.setData({
      peoplecount: e.detail.value,
    });
    console.log(that.data.peoplecount)
  },

  inputchangedaycount: function (e) {
    var that = this
    console.log(e)

    that.setData({
      daycount: e.detail.value,
    });
  },

  /**
   * 监听时间picker选择器
   */
  listenerstartTimePickerSelected: function (e) {
    var that = this
    //调用setData()重新绘制
    this.setData({
      starttime: e.detail.value,
    });
  },

  /**
  * 监听时间picker选择器
  */
  listenerendTimePickerSelected: function (e) {
    var that = this
    //调用setData()重新绘制
    this.setData({
      endtime: e.detail.value,
    });
  },

  /**
   * 监听日期picker选择器
   */
  listenerstartDatePickerSelected: function (e) {
    var that = this
    that.setData({
      startdate: e.detail.value
    })

    if (that.data.startdate > that.data.enddate) {
      that.setData({
        enddate: that.data.startdate,
        daycount: 1
      })
    } else {
      var stringTime = that.data.enddate + ' 00:00:00';
      var stringTime2 = that.data.startdate + ' 00:00:00';
      stringTime = stringTime.replace(/-/g, '/');
      stringTime2 = stringTime2.replace(/-/g, '/');
      var timestamp = Date.parse(new Date(stringTime));
      var timestamp2 = Date.parse(new Date(stringTime2));
      timestamp = timestamp / 1000;
      timestamp2 = timestamp2 / 1000;

      console.log(timestamp)
      console.log(timestamp2)

      var days = timestamp - timestamp2
      console.log(days)

      if (days == 0) {
        that.setData({
          daycount: 1
        })
      } else {
        days = days / 86400 + 1
        console.log(days)
        that.setData({
          daycount: days
        })
      }
    }
  },

  /**
   * 监听日期picker选择器
   */
  listenerendDatePickerSelected: function (e) {
    var that = this
    that.setData({
      enddate: e.detail.value
    })

    if (that.data.enddate < that.data.startdate) {
      that.setData({
        startdate: that.data.enddate,
        daycount: 1
      })
    } else {
      var stringTime = that.data.enddate + ' 00:00:00';
      var stringTime2 = that.data.startdate + ' 00:00:00';
      stringTime = stringTime.replace(/-/g, '/');
      stringTime2 = stringTime2.replace(/-/g, '/');
      var timestamp = Date.parse(new Date(stringTime));
      var timestamp2 = Date.parse(new Date(stringTime2));
      timestamp = timestamp / 1000;
      timestamp2 = timestamp2 / 1000;

      console.log(timestamp)
      console.log(timestamp2)

      var days = timestamp - timestamp2
      console.log(days)

      if (days == 0) {
        that.setData({
          daycount: 1
        })
      } else {
        days = days / 86400 + 1
        console.log(days)
        that.setData({
          daycount: days
        })
      }
    }
  },

  inputchangeprice: function (e) {
    var that = this
    console.log(e)

    that.setData({
      routeprice: e.detail.value,
    });
  },

  routeroute: function (e) {
    var that = this
    console.log(e)

    that.setData({
      routeroute: e.detail.value,
    });
  },

  routeother: function (e) {
    var that = this
    console.log(e)

    that.setData({
      routeother: e.detail.value,
    });
  },

  includepriceradio: function (e) {
    var that = this
    console.log(e)
    var include = e.currentTarget.dataset.id
    if (include == 'NO') {
      that.setData({
        includeprice: false
      });
    } else {
      that.setData({
        includeprice: true
      });
    }

    console.log(that.data.includeprice)
  },

  calldriver: function () {
    var that = this
    var phoneNumber = that.data.usecarcompanutel
    console.log(phoneNumber)

    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },



  send: function () {
    var that = this
    that.setData({
      sendhidden: true
    });

    if (that.data.routetitle.length > 50) {
      that.showError("行程简介不能超过50个字")
      that.setData({
        sendhidden: false
      });
      return
    }

    if (!that.isInteger(that.data.daycount) || !that.isInteger(that.data.routeseatcount) || !that.isInteger(that.data.peoplecount)) {
      that.showError("请填写整数")
      that.setData({
        sendhidden: false
      });
      return
    }

    if (!that.beforesend()) {
      console.log('发送失败')
      that.setData({
        sendhidden: false
      });
    } else {

      wx.showToast({
        title: '订单存储中',
        icon: 'loading',
        duration: 10000
      })

      var useinfo = wx.getStorageSync('userInfo')
      var useid = useinfo.ZID
      // var hanfei = ''
      if (that.data.includeprice == false) {
        that.data.includeprice = 0
      }
      if (that.data.includeprice == true)
        that.data.includeprice = 1

      if (that.data.usecarcomcansee == false) {
        that.data.usecarcomcansee = 0
      }
      if (that.data.usecarcomcansee == true) {
        that.data.usecarcomcansee = 1
      }


      if (that.data.includeprice == 1) {
        that.data.includeprice = '含费'
      } else {
        that.data.includeprice = '不含费'
      }
      // var showuser = ''
      if (that.data.usecarcomcansee == 1) {
        that.data.usecarcomcansee = '1'
      } else {
        that.data.usecarcomcansee = '0'
      }

      if (that.data.timeCheck1 == false) {
        that.data.starttime = '00:00'
      }

      if (that.data.timeCheck2 == false) {
        that.data.endtime = '23:59'
      }

      var startime = that.data.startdate + ' ' + that.data.starttime + ':00'
      var endtime = that.data.enddate + ' ' + that.data.endtime + ':00'
      // var endtime = that.data.enddate + ' 23:59:59'

      if (that.data.pagetype == 1) {
        wx.request({
          url: 'https://www.lanshuicar.com/Server/CarCompany/AddMyOrders.ashx',
          data: {
            ID: useid,
            xingchengjianjie: that.data.routetitle,
            yongchefang: that.data.usecarcompanunamemain,
            yongchefangtel: that.data.usecarcompanutelmain,
            ycConnector: that.data.usecarcompanuperson2,
            lxsid: that.data.lxsid,
            renshu: that.data.peoplecount,
            zuoweishu: that.data.routeseatcount,
            yusuanchejia: that.data.routeprice,
            hanfei: that.data.includeprice,
            yongcheriqi1: startime,
            yongcheriqi2: endtime,
            yujitianshu: that.data.daycount,
            shangchedidian: that.data.upplace,
            xiachedidian: that.data.downplace,
            xingchengneirong: that.data.routeroute,
            qitayaoqiu: that.data.routeother,
            showuser: that.data.usecarcomcansee,
          },
          method: 'GET',
          success: function (res) {

            if (res.statusCode == 404) {
              //接口加载失败
              console.log('加载接口错误');
              //显示错误信息
              that.showError('新增订单接口请求失败')
              //隐藏Toast
              wx.hideToast()
              that.setData({
                loadError: true,
                sendhidden: false
              });
              return
            }
            if (res.statusCode == 500) {
              //接口加载失败
              console.log('加载接口错误');
              //显示错误信息
              that.showError('上下车地点不能超过25个字')
              //隐藏Toast
              wx.hideToast()
              that.setData({
                loadError: true,
                sendhidden: false
              });
              return
            }

            var data = tool.getRegularJson(res.data);
            console.log(data)

            if (data.code == 0) {
              //发送成功
              wx.hideToast()
              wx.navigateBack();
              wx.showToast({
                title: '新增订单成功',
                icon: 'success',
                duration: 2000
              })
              var idd = data.data[0].idd
              var BillNumber = data.data[0].PlatformBillNumber
              console.log(idd)
              console.log(BillNumber)
              // 推送功能
              that.goPush(idd)

              var pages = getCurrentPages()
              var backpage = pages[pages.length - 2]
              backpage.setData({
                backtag: 1,
              });

            } else {
              //发送失败
              that.showError(data.msg)
              wx.hideToast()
              that.setData({
                sendhidden: false
              });
            }
          },
          fail: function () {
            console.log('加载错误');
            that.showError('网络错误')
            wx.hideToast()
            that.setData({
              sendhidden: false
            });
          }
        })
      } else if (that.data.pagetype == 2) {
        //将数据放入一个数组，在保存的时候判断是否有过订单变动
        var arrcompare = []
        arrcompare = arrcompare.concat(that.data.routetitle)
        arrcompare = arrcompare.concat(that.data.usecarcompanunamemain)
        arrcompare = arrcompare.concat(that.data.usecarcompanutelmain)
        arrcompare = arrcompare.concat(that.data.usecarcomcansee)
        arrcompare = arrcompare.concat(that.data.upplace)
        arrcompare = arrcompare.concat(that.data.downplace)
        arrcompare = arrcompare.concat(that.data.routeseatcount)
        arrcompare = arrcompare.concat(that.data.peoplecount)
        arrcompare = arrcompare.concat(that.data.daycount)
        arrcompare = arrcompare.concat(that.data.startdate)
        arrcompare = arrcompare.concat(that.data.starttime)
        arrcompare = arrcompare.concat(that.data.enddate)
        arrcompare = arrcompare.concat(that.data.endtime)
        arrcompare = arrcompare.concat(that.data.routeprice)
        arrcompare = arrcompare.concat(that.data.routeroute)
        arrcompare = arrcompare.concat(that.data.routeother)
        arrcompare = arrcompare.concat(that.data.includeprice)
        that.data.arrlatest.push(arrcompare)
        console.log(that.data.arrlatest)

        //初始的用车时间和行程，标题
        var dataAndroutelatest = []
        var dateAndroute = []
        dateAndroute = dateAndroute.concat(that.data.startdate)
        dateAndroute = dateAndroute.concat(that.data.starttime)
        dateAndroute = dateAndroute.concat(that.data.enddate)
        dateAndroute = dateAndroute.concat(that.data.endtime)
        dateAndroute = dateAndroute.concat(that.data.routeroute)
        dateAndroute = dateAndroute.concat(that.data.routetitle)
        dataAndroutelatest.push(dateAndroute)

        //用json转成字符串
        var jarray1 = JSON.stringify(that.data.arrcompare2)
        var jarray2 = JSON.stringify(that.data.arrlatest)
        //未修改什么都不做
        if (jarray1 == jarray2) {
          console.log('未修改内容')
          that.data.sendTag = 0;
          wx.navigateBack();
          return
        }
        //修改过了就判断是改了什么再进行相关角色的推送
        else {
          console.log(that.data.dateAndroute1)
          console.log(dataAndroutelatest)
          var dateAndroutestr1 = JSON.stringify(that.data.dateAndroute1)
          var dateAndroutestr2 = JSON.stringify(dataAndroutelatest)
          if (dateAndroutestr1 == dateAndroutestr2) {
            console.log('没有变更时期和行程，不推送给司机')
          } else {
            console.log('日期和行程，标题变更了,推送给司机')
            //***************************
            that.getdriverandcar()
            //调用方法
            //***************************
          }

        }
        var renshu = ''
        if (that.data.peoplecount == '') {
          renshu = '0'
        } else {
          renshu = that.data.peoplecount
        }
        var zuoweishu = ''
        if (that.data.routeseatcount == '') {
          zuoweishu = '0'
        } else {
          zuoweishu = that.data.routeseatcount
        }

        wx.request({
          url: 'https://www.lanshuicar.com/Server/CarCompany/UpdateOrders.ashx',
          data: {
            IDD: that.data.IDD.replace('\'','’'),
            xingchengjianjie: that.data.routetitle.replace('\'', '’'),
            yongchefang: that.data.usecarcompanunamemain.replace('\'', '’'),
            yongchefangtel: that.data.usecarcompanutelmain.replace('\'', '’'),
            ycConnector: that.data.usecarcompanupersonmain.replace('\'', '’'),
            lxsid: that.data.lxsid.replace('\'', '’'),
            renshu: renshu.replace('\'', '’'),//that.data.peoplecount
            zuoweishu: zuoweishu.replace('\'', '’'),//that.data.routeseatcount
            yusuanchejia: that.data.routeprice.replace('\'', '’'),
            hanfei: that.data.includeprice.replace('\'', '’'),
            yongcheriqi1: startime,
            yongcheriqi2: endtime,
            yujitianshu: that.data.daycount,
            shangchedidian: that.data.upplace.replace('\'', '’'),
            xiachedidian: that.data.downplace.replace('\'', '’'),
            xingchengneirong: that.data.routeroute.replace('\'', '’'),
            qitayaoqiu: that.data.routeother.replace('\'', '’'),
            showuser: that.data.usecarcomcansee.replace('\'', '’'),
          },
          method: 'GET',
          success: function (res) {

            if (res.statusCode == 404) {
              //接口加载失败
              console.log('加载接口错误');
              //显示错误信息
              that.showError('更新订单接口请求失败')
              //隐藏Toast
              wx.hideToast()
              that.setData({
                loadError: true,
                sendhidden: false
              });
              return
            }

            if (res.statusCode == 500) {
              //接口加载失败
              console.log('加载接口错误');
              //显示错误信息
              that.showError('上下车地点不能超过25个字')
              //隐藏Toast
              wx.hideToast()
              that.setData({
                loadError: true,
                sendhidden: false
              });
              return
            }

            var data = tool.getRegularJson(res.data);
            console.log(data)

            if (data.code == 0) {
              //发送成功
              wx.navigateBack();
              var pages = getCurrentPages()
              var backpage = pages[pages.length - 3]
              backpage.setData({
                backtag: 1,
              });
              wx.showToast({
                title: '修改订单成功',
                icon: 'success',
                duration: 2000
              })
            } else {
              //发送失败
              wx.hideToast()
              that.showError(data.msg)
              that.setData({
                sendhidden: false
              });
            }
          },
          fail: function () {
            console.log('加载错误');
            wx.hideToast()
            that.showError('网络错误')
            that.setData({
              sendhidden: false
            });
          }
        })
      }
    }
  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  beforesend: function () {
    var that = this
    if (that.data.routetitle == '') {
      that.showError('请输入标题')
      return false
    }

    return true
  },

  onCancel: function () {
    var that = this
    that.setData({
      testboolean: false,
      delectfricom: true,
    });
  },

  hideModal2: function () {
    this.setData({
      testboolean: false,
      delectfricom: false,
      arroriginal: [],
      usecarcompanylist: []
    });
  },

  delectfriendcom: function (e) {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    var tel = useinfo.Telphone
    var pwd = wx.getStorageSync('password');
    console.log(e)
    var lxsname = e.currentTarget.dataset.idx
    var lxsid = e.currentTarget.dataset.lxsid
    wx.showModal({
      title: '确认删除',
      content: '您确认删除 ' + lxsname + ' 吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'https://www.lanshuicar.com/Server/CarCompany/LxsPtners.ashx',
            data: {
              tel: tel,
              pwd: pwd,
              lxsid: lxsid,
              act: 'del',
            },
            method: 'GET',
            success: function (res) {
              var data = tool.getRegularJson(res.data);
              console.log(data)

              if (data.code == 0) {
                //发送成功
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000
                })
                that.hideModal2()
              } else {
                //发送失败
                that.showError(data.msg)
              }
            },
            fail: function () {
              console.log('加载错误');
            }
          })
        } else {
          console.log('用户点击取消')
          that.hideModal2()
        }

      }
    })
  },

  hideModal3: function () {
    this.setData({
      testboolean: false,
      addfricom: false,
      arroriginal: [],
      usecarcompanylist: []
    });
  },

  confirmadd: function () {
    this.setData({
      testboolean: false,
      addfricom: true,
    });
  },

  inputchangeforadd: function (e) {
    var that = this
    console.log(e)

    that.setData({
      inputchangeforadd: e.detail.value,
    });
  },

  addfriendcom: function () {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    var tel = useinfo.Telphone
    var pwd = wx.getStorageSync('password');

    if (that.data.inputchangeforadd == '') {
      that.showError('请输入搜索关键字')
      return
    }

    wx.request({
      url: 'https://www.lanshuicar.com/Server/CarCompany/LxsPtners.ashx',
      data: {
        tel: tel,
        pwd: pwd,
        act: 'getall',
        keywords: that.data.inputchangeforadd
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {
          that.setData({
            addfriendcomplist: that.data.addfriendcomplist.concat(data.data),
            addfricom: false,
            addfricom2: true,
          });

          that.setData({
            arroriginaladdfriendlist: that.data.addfriendcomplist,
          });
        } else {
          wx.showToast({
            title: '搜索结果为空',
            icon: 'none'
          })
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  //输入框变化搜索
  inputchange2: function (e) {
    var that = this
    var arr = []
    var keyWord = e.detail.value

    for (var i = 0; i < that.data.arroriginaladdfriendlist.length; i++) {
      if (that.data.arroriginaladdfriendlist[i].CompanyName.indexOf(keyWord) >= 0) {
        arr.push(that.data.arroriginaladdfriendlist[i]);
      }
    }
    if (keyWord != null && keyWord != undefined && keyWord != '' && keyWord != ' ' && keyWord.length != 0) {
      that.setData({
        addfriendcomplist: arr,
      });
    } else {
      that.setData({
        addfriendcomplist: that.data.arroriginaladdfriendlist
      });
    }
  },

  addfriendcom2: function (e) {
    console.log(e)
    var that = this
    var name = e.currentTarget.dataset.idx
    var id = e.currentTarget.dataset.lxsid

    var useinfo = wx.getStorageSync('userInfo')
    var tel = useinfo.Telphone
    var pwd = wx.getStorageSync('password');

    wx.showModal({
      title: '确认添加',
      content: '您确认添加 ' + name + ' 为您的合作伙伴吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'https://www.lanshuicar.com/Server/CarCompany/LxsPtners.ashx',
            data: {
              tel: tel,
              pwd: pwd,
              lxsid: id,
              act: 'add',
            },
            method: 'GET',
            success: function (res) {
              var data = tool.getRegularJson(res.data);
              console.log(data)

              if (data.code == 0) {
                //发送成功
                wx.showToast({
                  title: '添加成功',
                  icon: 'success',
                  duration: 2000
                })
                that.hideModal4()
              } else {
                //发送失败
                that.showError(data.msg)
              }
            },
            fail: function () {
              console.log('加载错误');
            }
          })
        } else {
          console.log('用户点击取消')
          that.hideModal4()
        }
      }
    })
  },

  hideModal4: function () {
    this.setData({
      testboolean: false,
      addfricom: false,
      arroriginal: [],
      usecarcompanylist: [],
      arroriginaladdfriendlist: [],
      addfriendcomplist: [],
      addfricom2: false
    });
  },

  testpush: function () {

  },

  call: function (e) {
    console.log(e)
    var that = this
    var phoneNumber = e.currentTarget.dataset.phone
    console.log(phoneNumber)
    if (phoneNumber == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  inputusercarname: function (e) {
    var that = this
    var str = e.detail.value
    that.setData({
      usecarcompanuname2: str
    })
    // if (that.data.usecarcompanuname == '') {
    //   that.setData({
    //     lxsid: ''
    //   })
    // }
    console.log(that.data.lxsid)
    console.log(that.data.usecarcompanuname2)
  },
  inputusercartel: function (e) {
    var that = this
    var str = e.detail.value
    that.setData({
      usecarcompanutel2: str
    })
    console.log(that.data.usecarcompanutel2)
  },

  inputusercarperson: function (e) {
    var that = this
    var str = e.detail.value
    that.setData({
      usecarcompanuperson2: str
    })
    console.log(that.data.usecarcompanuperson2)
  },
  //新订单推送方法
  goPush: function (idd) {
    var that = this
    if (that.data.usecarcompanutel.length == 0) {
      return
    }
    wx.showToast({
      title: '正在加载...',
      icon: 'loading',
      duration: 10000
    });

    wx.request({
      url: 'https://www.lanshuicar.com/Server/Weixin/WxPushHdl.ashx',
      data: {
        idd: idd,
        tel: that.data.usecarcompanutel,
        title: '您有一个新订单',
        content: '车公司下单'
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        // var data = tool.getRegularJson(res.data);
        var code = res.data.code;
        console.log(res);
        if (code == 0) {
          console.log('post推送请求成功')

        } else {
          console.log(res)
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function () {
        // complete
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    })
  },

  //获取订单有关司机和车辆安排
  getdriverandcar: function () {
    var that = this
    var driverlist = []

    wx.request({
      url: 'https://www.lanshuicar.com/Server/DriverPerson/GetDriverCarInfo.ashx',
      data: {
        IDD: that.data.IDD,
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {

          driverlist = driverlist.concat(data.data)
          for (var i = 0; i < driverlist.length; i++) {
            that.ChangegoPush(driverlist[i].sTel)
            console.log(driverlist[i].sTel)
          }

          // console.log(pushdriverlist)
          // ChangegoPush
        }
      },
      fail: function () {

      }
    })
  },

  //修改订单推送方法
  ChangegoPush: function (telNum) {
    var that = this
    // if (that.data.usecarcompanutel.length == 0) {
    //   return
    // }
    wx.showToast({
      title: '正在加载...',
      icon: 'loading',
      duration: 10000
    });

    wx.request({
      url: 'https://www.lanshuicar.com/Server/Weixin/WxPushHdl.ashx',
      data: {
        idd: that.data.IDD,
        tel: telNum,
        title: '您有一个修改订单',
        content: '订单用车时间或者车辆行程有变更，请您及时关注'
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        // var data = tool.getRegularJson(res.data);
        var code = res.data.code;
        console.log(res);
        if (code == 0) {
          console.log('post推送请求成功')

        } else {
          console.log(res)
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function () {
        // complete
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    })
  },


  //隐藏添加车公司
  hideModalCompanyAdd: function () {


    this.setData({
      selectCompanyAdd: 0,
    });
  },

  //添加车公司弹框
  getusecarcompanymain: function () {
    this.setData({
      usecarcompanuperson: '',
      usecarcompanutel: '',
      usecarcompanuname: '',
      lxsidSecondary: '',
      usecarcompanuperson2: '',
      usecarcompanutel2: '',
      usecarcompanuname2: '',
      selectCompanyAdd: 1,
    });
  },
  //确定用车单位
  confirmusercompany: function () {
    if (this.data.usecarcompanuname.length > 0) {
      if (this.data.usecarcompanuperson2.length > 0 || this.data.usecarcompanutel2.length > 0 || this.data.usecarcompanuname2.length > 0) {
        this.showError('请勿都输入内容')
        return
      }

      this.setData({
        usecarcompanupersonmain: this.data.usecarcompanuperson,
        usecarcompanutelmain: this.data.usecarcompanutel,
        usecarcompanunamemain: this.data.usecarcompanuname,
        lxsid: this.data.lxsidSecondary,
        selectCompanyAdd: 0,
      })
    } else {
      this.setData({
        usecarcompanupersonmain: this.data.usecarcompanuperson2,
        usecarcompanutelmain: this.data.usecarcompanutel2,
        usecarcompanunamemain: this.data.usecarcompanuname2,
        lxsid: '',
        selectCompanyAdd: 0,
      })
    }
    console.log(this.data.lxsid)
  },

  isInteger: function (obj) {
    return obj % 1 === 0
  }
})